package com.example.temp;


import java.util.ArrayList;

public class Test31 {

    public static void main(String[] args) {

    }

    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
    }


    /**
     * 给定一个二叉树，返回该二叉树层序遍历的结果，（从左到右，一层一层地遍历）
     */


    public class Solution {

        public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
            // write code here
            ArrayList arrayList = new ArrayList<Integer>();
            ArrayList ans = new ArrayList<Integer>();
            arrayList.add(root);

            while (arrayList.size() > 0) {
                TreeNode temp = (TreeNode) arrayList.get(0);
                ans.add(temp);

                arrayList.remove(0);
                if (temp.right != null) {
                    arrayList.add(temp.right);
                }

                if (temp.left != null) {
                    arrayList.add(temp.left);
                }

            }
            return ans;

        }

    }
}
